//
//  XInfomationTableCell.m
//  Bees
//
//  Created by Xiaohan on 2018/12/21.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XInfomationTableCell.h"
#import "InformationModel.h"
@implementation XInfomationTableCell

- (void)setModel:(InformationModel *)model{
    _model = model;
    self.timeLab.text = [NSString stringWithFormat:@" %@ ",model.online_time];
    self.subTitleLab.text = model.title;
    self.contentLab.text = model.content;
    self.nameLab.text = [NSString stringWithFormat:@"%@:%@",XLocalized(@"作者"),model.author];
    if (model.cover.length) {
        [self.contentImgView sd_setImageWithURL:String_To_URL(model.cover)completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            // 等比例计算尺寸
            if (!model.coverImgH) {
                CGFloat pWidth = image.size.width < kScreenWidth-AdaptedWidth(30) ? image.size.width:kScreenWidth-AdaptedWidth(30);
                CGFloat pHeight = image.size.height * pWidth / image.size.width;
                model.coverImgH = pHeight;
                !self.DidDownLoadBlock?:self.DidDownLoadBlock(self.indexPath);
            }
        }];
    }else{
        self.contentImgView.image = [UIImage new];
        model.coverImgH = 0;
        !self.DidDownLoadBlock?:self.DidDownLoadBlock(self.indexPath);
    }
}

- (void)setupUI{
    self.timeLab = [UILabel new];
    self.timeLab.font = kDefaultFont(11);
    self.timeLab.textColor = UIColorHex(#FFFFFF);
    self.timeLab.backgroundColor = UIColorHex(#DEDEDE);
    ViewRadius(self.timeLab, AdaptedWidth(5.0f));
    [self.contentView addSubview:self.timeLab];
   
    self.backView = [[UIView alloc]initWithFrame:CGRectZero];;
    [self.contentView addSubview:self.backView];
  
    self.backView.backgroundColor = UIColor.whiteColor;
    self.backView.layer.cornerRadius = AdaptedWidth(10.0f);
    [self.backView setLayerShadow:kShadowColor offset:CGSizeMake(0, 0) radius:6];
    
//    self.titleLab = [UILabel new];
//    self.titleLab.font = kBoldFont(13);
//    self.titleLab.numberOfLines = 1;
//    self.titleLab.backgroundColor = kColorMain;
//    self.titleLab.textColor = UIColor.whiteColor;
//    [self.backView addSubview:self.titleLab];
//    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(AdaptedWidth(13));
//        make.left.mas_equalTo(AdaptedWidth(15));
//        make.height.mas_equalTo(AdaptedWidth(0.01));
//    }];
//    ViewRadius(self.titleLab, AdaptedWidth(9.0f));
//    self.titleLab.hidden = YES;
    self.subTitleLab = [UILabel new];
    self.subTitleLab.numberOfLines = 0;
    self.subTitleLab.font = kDefaultFont(15);
    self.subTitleLab.textColor = kTitleColor;
    [self.backView addSubview:self.subTitleLab];
  
    
    self.contentLab = [UILabel new];
    self.contentLab.numberOfLines = 0;
    self.contentLab.font = kDefaultFont(12);
    self.contentLab.textColor = kColorTextGray;
    [self.backView addSubview:self.contentLab];
   
    
    self.contentImgView = [UIImageView new];
    ViewRadius(self.contentImgView, AdaptedWidth(10.0f));
    self.contentImgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.backView addSubview:self.contentImgView];
    
    self.nameLab = [UILabel new];
    self.nameLab.font = kDefaultFont(12);
    self.nameLab.textColor = kColorTextGray;
    [self.backView addSubview:self.nameLab];

    
    self.arrowBtn = [UIButton new];
    [self.arrowBtn setImage:IMAGE_NAMED(@"yellow_share") forState:UIControlStateNormal];
    [self.backView addSubview:self.arrowBtn];
   
    kWeakSelf(self);
    [self.arrowBtn addTapBlock:^(UIButton * _Nonnull btn) {
        !weakself.ShareBlock?:weakself.ShareBlock(weakself.model);
    }];
    
    [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kNormalSpace);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(AdaptedWidth(19));
    }];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeLab.mas_bottom).offset(AdaptedWidth(14.0f));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.bottom.mas_equalTo(-kNormalSpace);
    }];
    [self.subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView.mas_top).offset(AdaptedWidth(11));
        make.left.mas_equalTo(AdaptedWidth(15));
        make.right.mas_equalTo(-AdaptedWidth(15));
    }];
    [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.subTitleLab.mas_bottom).offset(AdaptedWidth(19));
        make.left.equalTo(self.subTitleLab.mas_left);
        make.right.equalTo(self.subTitleLab.mas_right);
        make.bottom.equalTo(self.contentImgView.mas_top).offset(AdaptedWidth(-8));
    }];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.subTitleLab.mas_left);
        make.right.equalTo(self.arrowBtn.mas_left).offset(-AdaptedWidth(15));
        make.bottom.mas_equalTo(-AdaptedWidth(15));
        make.height.mas_equalTo(AdaptedWidth(19));
    }];
    [self.contentImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.subTitleLab.mas_left);
        make.right.equalTo(self.subTitleLab.mas_right);
        make.bottom.equalTo(self.nameLab.mas_top).offset(AdaptedWidth(-8));
    }];
    [self.arrowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLab.mas_centerY);
        make.right.equalTo(self.subTitleLab.mas_right);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
}

@end
